package lifeOfAFish;

import lifeOfAFish.test.JoglTemplate;
import lifeOfAFish.scenegraph.*;

import java.awt.event.KeyEvent;
import java.io.File;
import java.nio.IntBuffer;

import javax.media.opengl.*;
//import javax.media.opengl.GLAutoDrawable;

import com.sun.opengl.util.BufferUtil;
import com.sun.opengl.util.texture.Texture;
import com.sun.opengl.util.texture.TextureIO;



public class MainSimulation extends JoglTemplate {
	
	// the scene
	private Scene scene = null;
	private boolean day = true;
	
	
	public static void main (String[] args){
		MainSimulation template = new MainSimulation();
		template.setVisible(true);
		
	}

	public void init(GLAutoDrawable drawable)
	{
		super.init(drawable);
		
		// get gl object
		GL gl = drawable.getGL();
		sgGlobals.gl = gl;
		
		// enable depth testing
		gl.glEnable(GL.GL_DEPTH_TEST);
		gl.glDepthRange(0.0, 1000.0);
		
		// enable lighting
		gl.glEnable(GL.GL_LIGHTING);
//		gl.glShadeModel(GL.GL_SMOOTH);
		gl.glShadeModel(GL.GL_PHONG_WIN);
		
		gl.glEnable(GL.GL_TEXTURE_2D);
		gl.glEnable(GL.GL_BLEND);
//		gl.glEnable(GL.GL_COLOR_MATERIAL);
		gl.glBlendFunc(GL.GL_SRC_ALPHA,GL.GL_ONE_MINUS_SRC_ALPHA);
		gl.glLightModeli(GL.GL_LIGHT_MODEL_COLOR_CONTROL, GL.GL_SEPARATE_SPECULAR_COLOR);
		
		// fog
		gl.glEnable(GL.GL_FOG);
		{
			float[] fogCol = {0.5f, 0.5f, 0.5f, 1.0f};
			gl.glFogi(GL.GL_FOG_MODE, GL.GL_EXP2);
			gl.glFogfv(GL.GL_FOG_COLOR, fogCol, 0);
			gl.glFogf(GL.GL_FOG_DENSITY, 0.04f);
			gl.glHint(GL.GL_FOG_HINT,GL.GL_DONT_CARE);
			gl.glFogf(GL.GL_FOG_START, 1.0f);
			gl.glFogf(GL.GL_FOG_END, 5.0f);
		}
		
		// create the elements of the scene
		if (scene == null){
			scene = new Scene();
			scene.init();
		}
		sgGeomBlender.getInstance().setDebug(false);
		
	}
	
	/*
	 * overwritten version of display
	 */
	public void display(GLAutoDrawable drawable)
	{
		// get the gl object
		GL gl = drawable.getGL();
		// set the erasing color (black)
//		gl.glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
		gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
		// clear screen with the defined erasing color and depth buffer
		gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
		gl.glPushMatrix();
		applyMouseTranslation(gl);
		applyMouseRotation(gl);

		// render the scene
		scene.update();
		
		// load an object using the mesh loader
		gl.glPopMatrix();
	}
	private void resetView()
	{
		view_rotx = 0.0f; view_roty = 0.0f; view_rotz = 0.0f;
		view_transx = 0.0f; view_transy = 0.0f; view_transz = -0.0f;
	}
	
	public void keyPressed(KeyEvent e)
	{
		super.keyPressed(e);
		
		if (e.getKeyCode() == KeyEvent.VK_1){
			scene.setView(1);
			resetView();
		}
		else if (e.getKeyCode() == KeyEvent.VK_2){
			scene.setView(2);
			resetView();
		}
		else if (e.getKeyCode() == KeyEvent.VK_3){
			scene.setView(3);
			resetView();
		}
		else if (e.getKeyCode() == KeyEvent.VK_4){
			scene.setView(4);
			resetView();
		}
		else if (e.getKeyCode() == KeyEvent.VK_5){
			scene.setView(5);
			
			view_rotx = 0.0f; view_roty = 0.0f; view_rotz = 0.0f;
			view_transx = 0.0f; view_transy = -2.0f; view_transz = -0.0f;
		}
		
		
		
		else if (e.getKeyCode() == KeyEvent.VK_6){
			scene.setView(6);
			resetView();
		}
		
		
		
		else if (e.getKeyCode() == KeyEvent.VK_D){
			scene.setDay();
		}
		
		else if (e.getKeyCode() == KeyEvent.VK_N){
			scene.setNight();
		}
	}
	
	public void reshape(GLAutoDrawable drawable, int x, int y, int width,
			int height)
	{
		// get a gl object
		GL gl = drawable.getGL();
		// set the OpenGL Viewport to the actual width and height of the window
		gl.glViewport(0, 0, width, height);
		// choose your type of projection(ortho or perspective)
		// choose the projection matrix-mode
		gl.glMatrixMode(GL.GL_PROJECTION);
		// and load it with the identity matrix
		gl.glLoadIdentity();
		// perspective projection
		sgGlobals.glu.gluPerspective(scene.getViewingAngle(), (float) width / height, 0.1f, 100);
		// make sure to use the modelview matrix-mode
		gl.glMatrixMode(GL.GL_MODELVIEW);
		// load the identity matrix as the modelview (resets all previous
		// transformations)
		gl.glLoadIdentity();
	}
	
	
}
